
package com.longai.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import util.Page;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.longai.model.News;
import com.longai.model.Newscomment;
import com.longai.service.NewsService;
import com.longai.service.NewscommentService;
import com.longai.service.UserService;
import com.longai.vo.NewsCommentVo;

@Controller
@RequestMapping("newscomment")
public class NewscommentAction {
    @Autowired
    NewscommentService service;
    @Autowired
    UserService userService;
    @Autowired
    NewsService newsService;
    
	@RequestMapping(value = "commentNews",method = RequestMethod.GET)//获取app的NewcommentVo实体
	@ResponseBody
	public Object commentNews(
            @RequestParam(value = "comment") String comment){
			Newscomment uNewscomment=new Newscomment();
			try {
				Gson gson=new Gson();
				NewsCommentVo newscommentVo=gson.fromJson(comment, NewsCommentVo.class);
				Newscomment newscomment=new Newscomment();
				newscomment.setCommentContet(newscommentVo.getCommentContent());
				newscomment.setUser(userService.findById(newscommentVo.getUserId()));
				newscomment.setNews(newsService.findById(newscommentVo.getNewsId()));
				newscomment.setPubtime(newscommentVo.getPubtime());
				newscomment.setNewscommentId(newscommentVo.getNewsCommentId());
				service.save(newscomment);
				return newscommentVo;
			} catch (Exception e) {
				// TODO: handle exception
				return uNewscomment;
			}		
	}
	
	@RequestMapping(value = "commentsByNewsId",method = RequestMethod.GET)
	@ResponseBody
	public Object commentsByNewsId(@RequestParam("newsId") String newsid,
			@RequestParam("currentPage") String currentPage,
			@RequestParam("amount") String amount){
			Page u=service.findByNewid(newsid,currentPage,amount);
			List<Newscomment> g=u.getList();
			List<NewsCommentVo> s= new ArrayList();
			if(!g.isEmpty()){
			for(Newscomment n:g){
				NewsCommentVo vo=new NewsCommentVo();
				vo.setCommentContent(n.getCommentContet());
				vo.setNewsCommentId(n.getNewscommentId());
				vo.setNewsId(n.getNews().getNewsId());
				vo.setPubtime(n.getPubtime());
				vo.setUserId(n.getUser().getUserId());
				vo.setNickname(n.getUser().getNickname());
				s.add(vo);
			}
			u.setList(s);
			return u;
			}
			// TODO: handle exception
			Newscomment gg=new Newscomment();
			return gg;		
	}
}

